package com.auto.survey.activity.monitoring.entity

import android.text.TextUtils
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey

/**
 * 测点实体类,每个测点的核验记录
 */
@Entity(
    tableName = "MonitoringCheckPoint",
    foreignKeys = [
        ForeignKey(
            entity = MonitoringPoint::class,
            parentColumns = ["id"],
            childColumns = ["pointId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["pointId"])]
)
data class MonitorCheckPoint(
    /**
     * 测点ID，主键，自增长
     */
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    /**
     * 所属监测测点ID
     */
    var pointId: Int,
    /**
     * 测点X坐标
     */
    var checkPointX: Double,
    /**
     * 测点Y坐标
     */
    var checkPointY: Double,
    /**
     * 测点Z坐标（高程）
     */
    var checkPointZ: Double,
    /**
     * 水平角
     */
    var checkPointHz:Double,
    /**
     * 垂直角
     */
    var checkPointV:Double,
    /**
     * 斜距
     */
    var checkPointSD:Double,
    /**
     * 是否已经核验
     */
    var isChecked:Boolean,
    /**
     * 创建时间，格式：YYYY-MM-DD HH:MM:SS
     */
    var createTime: String,
    /**
     * 核验是否通过
     */
    var isPass:Boolean,

    /**
     * 做程序逻辑用，不存数据库，代表是不是一个新的对象
     */
    var isNew:Boolean
) {

    fun setEntityData(checkPoint: MonitorCheckPoint) {
        this.checkPointX = checkPoint.checkPointX
        this.checkPointY = checkPoint.checkPointY
        this.checkPointZ = checkPoint.checkPointZ

        this.checkPointHz = checkPoint.checkPointHz
        this.checkPointV = checkPoint.checkPointV
        this.checkPointSD = checkPoint.checkPointSD

        this.isChecked = checkPoint.isChecked
        this.createTime = checkPoint.createTime
        this.isPass = checkPoint.isPass
        this.isNew = checkPoint.isNew
    }

    fun merge(other: MonitorCheckPoint): MonitorCheckPoint{
        return this.copy(
            id = other.id,
            pointId = other.pointId ?: this.pointId,
            checkPointX = other.checkPointX ?: this.checkPointX,
            checkPointY = other.checkPointY ?: this.checkPointY,
            checkPointZ = other.checkPointZ ?: this.checkPointZ,
            checkPointHz = other.checkPointHz ?: this.checkPointHz,
            checkPointV = other.checkPointV ?: this.checkPointV,
            checkPointSD = other.checkPointSD ?: this.checkPointSD,
            isChecked = other.isChecked ?: this.isChecked,
            createTime = other.createTime ?: this.createTime,
            isPass = other.isPass ?: this.isPass,
            isNew = other.isNew ?: this.isNew)
    }

    /**
     * 判断是否所有必要属性都已赋值（根据业务定义“完整”的标准）
     */
    fun isAllPropertiesComplete(): Boolean {
        // 示例：假设 id、name、x、y、z 是必须的属性
        return pointId != -1
                && checkPointX != -10000000000.0
                && checkPointY != -10000000000.0
                && checkPointZ != -10000000000.0
                && checkPointHz != -10000000000.0
                && checkPointV != -10000000000.0
                && checkPointSD != -10000000000.0
                && TextUtils.isEmpty(createTime)
                && isChecked
                && isNew
    }
}